package flex.cc.nursing.controller;

import com.alibaba.fastjson.JSONObject;
import flex.cc.basicinformation.service.StoreActivityConfigService;
import flex.cc.core.support.BaseController;
import flex.cc.nursing.dto.entity.NursingOrderEntity;
import flex.cc.nursing.dto.entity.NursingSynStatusEntity;
import flex.cc.nursing.service.NursingOrderService;
import flex.cc.nursing.service.NursingSynStatusService;
import flex.cc.store.dto.entity.StoreActivityConfigEntity;
import flex.cc.system.entity.DictCodeEntity;
import flex.cc.system.service.DictCodeService;
import flex.cc.system.vo.DictOptionVo;
import flex.cc.wrapper.WrapMapper;
import flex.cc.wrapper.Wrapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wkangle
 * @className NursingServiceController
 * @description 健康护理服务模块
 * @time 2022-03-23 13:36:12
 */
@SuppressWarnings("restriction")
@RestController
@RequestMapping(value = "/vehicleSvr/nursingService/wechat")
public class NursingServiceController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(NursingServiceController.class);

    @Autowired
    private NursingOrderService nursingOrderService;
    @Autowired
    private NursingSynStatusService nursingSynStatusService;
    @Autowired
    private DictCodeService dictCodeService;
    @Autowired
    private StoreActivityConfigService storeActivityConfigService;

    /**
     * @description 护理类卡券根据服务项目查询对应标签信息
     * @param activityId 服务项目
     * @return
     * @author wkangle
     * @time 2022/03/24 0024 14:14
     * @exception/throws
    */
    @GetMapping(value = "/initTags")
    @ApiOperation(httpMethod = "GET", value = "护理类卡券根据服务项目查询对应标签信息")
    public Wrapper<List<DictCodeEntity>> initTags(@ApiParam(name = "reqVo", value = "请求信息") @RequestParam(name = "activityId") String activityId) {
        logger.info("NursingServiceController -- initTags() ====> ", activityId);
        List<DictCodeEntity> list = nursingOrderService.queryNursingTags(activityId);
        return WrapMapper.ok(list);
    }

    @PostMapping(value = "/nursingSave")
    @ApiOperation(httpMethod = "POST", value = "确定预约提交")
    public Wrapper<JSONObject> nursingSave(@ApiParam(name = "reqVo", value = "请求信息") @RequestBody Map<String, Object> reqVo) {
        logger.info("NursingServiceController -- nursingSave() ====> ", reqVo);
        JSONObject appointment = nursingOrderService.createAppointment(reqVo);
        return WrapMapper.ok(appointment);
    }

    @PostMapping(value = "/nursingOrderInfo")
    @ApiOperation(httpMethod = "POST", value = "预约信息")
    public Wrapper<NursingOrderEntity> nursingOrderInfo(@ApiParam(name = "reqVo", value = "请求信息") @RequestBody Map<String, Object> reqVo) {
        logger.info("NursingServiceController -- nursingOrderInfo() ====> {}", reqVo);
        reqVo.put("orderId", reqVo.get("couponNum"));
        NursingOrderEntity nursingOrderEntity = nursingOrderService.queryOneByParams(reqVo);
        return WrapMapper.ok(nursingOrderEntity);
    }

    @PostMapping(value = "/nursingProgress")
    @ApiOperation(httpMethod = "POST", value = "服务进展")
    public Wrapper<NursingSynStatusEntity> nursingProgress(@ApiParam(name = "reqVo", value = "请求信息") @RequestBody Map<String, Object> reqVo) {
        logger.info("NursingServiceController -- nursingProgress() ====> {}", reqVo);
        NursingSynStatusEntity nursingSynStatusEntity = nursingSynStatusService.queryOneByParams(reqVo);
        return WrapMapper.ok(nursingSynStatusEntity);
    }

    @PostMapping(value = "/nursingOrderList")
    @ApiOperation(httpMethod = "POST", value = "服务记录")
    public Wrapper<List<NursingOrderEntity>> nursingOrderList(@ApiParam(name = "reqVo", value = "请求信息") @RequestBody Map<String, Object> reqVo) {
        logger.info("NursingServiceController -- nursingOrderList() ====> {}", reqVo);
        List<NursingOrderEntity> nursingOrderList = nursingOrderService.queryNursingOrderList(reqVo);
        return WrapMapper.ok(nursingOrderList);
    }

    /**
     * @description 校验服务包使用次数
     * @param couponNum 卡券编码
     * @return
     * @author wkangle
     * @time 2022-04-06 16:33:54
     * @exception/throws
     */
    @GetMapping(value = "/queryMaxTimes")
    @ApiOperation(httpMethod = "GET", value = "校验服务包使用次数")
    public Wrapper<Number> queryMaxTimes(@ApiParam(name = "reqVo", value = "请求信息") @RequestParam(name = "couponNum") String couponNum) {
        logger.info("NursingServiceController -- queryMaxTimes() ====> couponNum: {} ", couponNum);
        Number maxTimes = nursingOrderService.queryMaxTimes(couponNum);
        return WrapMapper.ok(maxTimes);
    }

    /**
     * 查询行政区划下拉选项的值
     * @return the wrapper
     */
    @GetMapping(value = "/getAdmOptionsByLeval/{leval}")
    @ApiOperation(httpMethod = "GET", value = "查询行政区划下拉选项的值")
    public Wrapper<Map<String, Map<String, String>>> getAdmOptionsByLeval(@PathVariable(value = "leval") String leval) {
        Map<String, Map<String, String>> result = new HashMap<>();

        List<DictOptionVo> pList =  dictCodeService.getAdmOptionsByLeval("1");
        Map<String, String> pMap = new HashMap<>();
        for (DictOptionVo vo : pList) {
            pMap.put(vo.getValue(), vo.getLabel());
        }

        List<DictOptionVo> cList =  dictCodeService.getAdmOptionsByLeval("2");
        Map<String, String> cMap = new HashMap<>();
        for (DictOptionVo vo : cList) {
            cMap.put(vo.getValue(), vo.getLabel());
        }
        List<DictOptionVo> coList =   dictCodeService.getAdmOptionsByLeval("3");
        Map<String, String> coMap = new HashMap<>();
        for (DictOptionVo vo : coList) {
            coMap.put(vo.getValue(), vo.getLabel());
        }
        result.put("province_list", pMap);
        result.put("city_list", cMap);
        result.put("county_list", coMap);

        return WrapMapper.ok(result);
    }
}
